package com.appspot.diasporajava.federation;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Category;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.model.Link;
import org.apache.abdera.parser.Parser;
import org.apache.abdera.parser.ParserOptions;
import org.apache.abdera.protocol.client.AbderaClient;
import org.apache.abdera.protocol.client.ClientResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pegdown.Extensions;
import org.pegdown.LinkRenderer;
import org.pegdown.PegDownProcessor;
import org.pegdown.ToHtmlSerializer;
import org.pegdown.ast.RootNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tautua.markdownpapers.Markdown;
import org.tautua.markdownpapers.parser.ParseException;

import com.appspot.diasporajava.controller.MainController;
import com.appspot.diasporajava.entities.Post;
import com.appspot.diasporajava.util.WebsiteVisitor;
import com.petebevin.markdown.MarkdownProcessor;

@Component
public class PublicFeed {
	private Log log = LogFactory.getLog(MainController.class);

	@Autowired
	WebsiteVisitor websiteVisitor;

	

	public static void main(String[] args) {
		AbderaClient client = new AbderaClient(Abdera.getInstance());
		ClientResponse response = client.get("https://joindiaspora.com/public/apolonisaphrodisia.atom");
		Document<Feed> doc = response.getDocument();
		Feed feed = doc.getRoot();
		// Get the feed title
		System.out.println("Feed Title: " + feed.getTitle());

		// Get the entry items...
		for (Entry entry : feed.getEntries()) {
			System.out.println("Title: " + entry.getTitle());
			System.out.println("Unique Identifier: " + entry.getId().toString());
			System.out.println("Updated Date: " + entry.getUpdated().toString());
			System.out.println("Published Date: " + entry.getPublished());
			System.out.println("Content: " + entry.getContent());

			// Get the links
			for (Link link : (List<Link>) entry.getLinks()) {
				System.out.println("Link: " + link.getHref());
			}

			// Get the categories
			for (Category category : (List<Category>) entry.getCategories()) {
				System.out.println("Category: " + category.getTerm());
			}

		}

	}

	/**
	 * Returns public posts subsequent to a certain date
	 * @param getEntryList
	 * @param date
	 * @return
	 */
	public List<Post> getPostList(String getEntryList, Date date) {
		List<Post> posts = new ArrayList<Post>();
		List<Entry> entries = getEntryList(getEntryList);

		for (Entry entry : entries) {
			Date edited = entry.getEdited();
			if (edited!=null && edited.compareTo(date) > 0) {
				Post post = new Post(entry);
				post.setHtml(MarkDown.transform(post.getMarkdown()));
				posts.add(post);
			}
		}
		return posts;
	}

	/**
	 * Returns all public posts
	 * @param getEntryList
	 * @return
	 */
	public List<Post> getPostList(String getEntryList) {
		List<Post> posts = new ArrayList<Post>();
		List<Entry> entries = getEntryList(getEntryList);

		for (Entry entry : entries) {
			Post post = new Post(entry);
			String markdown = post.getMarkdown();
			post.setHtml(MarkDown.transform(markdown));
			posts.add(post);
		}
		return posts;
	}

	private List<Entry> getEntryList(String atomFeedUrl) {
		Abdera abdera = Abdera.getInstance();
		Parser parser = abdera.getParser();
		//ParserOptions parserOptions = parser.getDefaultParserOptions();
		Document<Feed> doc = parser.parse(websiteVisitor.getInputStream(atomFeedUrl));
		Feed feed = doc.getRoot();

		return feed.getEntries();
	}

	public List<Entry> getContentList(String getEntryList) {
		List<Entry> entries = getEntryList(getEntryList);
		List<String> contentList = new ArrayList<String>();

		for (Entry entry : entries) {
			entry.setContent(MarkDown.transform(entry.getContent()));
		}

		return entries;
	}

	
}
